package com.tct.worktimeboard.feishu;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tct.worktimeboard.config.DataSourceContextHolder;
import com.tct.worktimeboard.entity.UserInfo;
import com.tct.worktimeboard.mapper.UserInfoMapper;
import com.tct.worktimeboard.mapper.WorkGroupMapper;
import com.tct.worktimeboard.util.JsonUtil;
import com.tct.worktimeboard.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Huhailong
 * @Description
 * @Date 2021/5/26.
 */
@Slf4j
@Service
public class FeiShuService {

    @Autowired
    SendMessage sendMessage;
    @Autowired
    FeiShuUtil feiShuUtil;
    @Resource
    WorkGroupMapper workGroupMapper;
    @Resource
    UserInfoMapper userInfoMapper;

    public JSONObject sendMessage(int sendType, String message){
        switch(sendType){
            case 1: //给所有人发送
                getAllUserMessageEntity(message);
                return JsonUtil.toJsonData();
            case 2: //给所有审批人发送
                getAllMakeUser(message);
                return JsonUtil.toJsonData();
            case 3: //给所有填报人发送
                getAddWorkTimeUser(message);
                return JsonUtil.toJsonData();
            default:
                return JsonUtil.toErrorData("请指定发送类型");
        }
    }

    public void getAllUserMessageEntity(String message){
        List<Long> allGroupUser = workGroupMapper.getAllGroupUser();
        List<Long> allPowerUser = workGroupMapper.getAllPowerUser();
        allGroupUser.addAll(allPowerUser);
        List<Long> userIds = allGroupUser.stream().distinct().collect(Collectors.toList());
        MessageEntity messageEntity = messageEntity(userIds, message);
        List<String> open_ids = messageEntity.getOpen_ids();
        List<List<String>> lists = ListUtil.cutApart(200, open_ids);
        log.info("分{}次发送",lists.size());
        for(List<String> list : lists){
            messageEntity.setOpen_ids(list);
            sendMessage.sendMessageBatch(messageEntity);
        }
    }

    public void getAddWorkTimeUser(String message){
        List<Long> allGroupUser = workGroupMapper.getAllGroupUser();
        List<Long> userIds = allGroupUser.stream().distinct().collect(Collectors.toList());
        MessageEntity messageEntity = messageEntity(userIds, message);
        List<String> open_ids = messageEntity.getOpen_ids();
        List<List<String>> lists = ListUtil.cutApart(200, open_ids);
        log.info("分{}次发送",lists.size());
        for(List<String> list : lists){
            messageEntity.setOpen_ids(list);
            sendMessage.sendMessageBatch(messageEntity);
        }
    }

    public void getAllMakeUser(String message){
        List<Long> allPowerUser = workGroupMapper.getAllPowerUser();
        List<Long> userIds = allPowerUser.stream().distinct().collect(Collectors.toList());
        MessageEntity messageEntity = messageEntity(userIds, message);
        List<String> open_ids = messageEntity.getOpen_ids();
        List<List<String>> lists = ListUtil.cutApart(200, open_ids);
        log.info("分{}次发送",lists.size());
        for(List<String> list : lists){
            messageEntity.setOpen_ids(list);
            sendMessage.sendMessageBatch(messageEntity);
        }
    }

    private MessageEntity messageEntity(List<Long> userIds, String message){
        DataSourceContextHolder.setContextHolder("db2");
        List<UserInfo> userInfoList = userInfoMapper.selectList(new QueryWrapper<UserInfo>().in("id", userIds));
        List<String> collect = userInfoList.stream().map(UserInfo::getPhone).collect(Collectors.toList());
        List<String> tels = collect.stream().filter(Objects::nonNull).collect(Collectors.toList());
        List<List<String>> lists = ListUtil.cutApart(50, tels);
        List<String> allOpenId = new ArrayList<>();
        for(List<String> list : lists){
            List<String> openIds = feiShuUtil.getUserInfoList(list);
            allOpenId.addAll(openIds);
        }
        DataSourceContextHolder.clear();
        MessageEntity messageEntity = new MessageEntity();
        Map<String,String> map = new HashMap<>();
        map.put("text",message);
        messageEntity.setOpen_ids(allOpenId);
        messageEntity.setMsg_type("text");
        messageEntity.setContent(JSONObject.toJSON(map));
        return messageEntity;
    }
}
